package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.mall.dto.ProductDetailDto;
import com.cskaoyan.mall.dto.ProductDetailRequest;
import com.cskaoyan.mall.dto.ProductDetailResponse;
import com.cskaoyan.shopping.constants.GlobalConstants;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.service.ICartService;
import com.cskaoyan.shopping.service.IProductService;

import com.cskaoyan.shopping.service.cache.CartCacheManager;
import com.cskaoyan.shopping.service.cache.ShoppingCacheManager;
import com.cskaoyan.shopping.utils.ExceptionProcessorUtils;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;



/**
 * 购物车实现类
 * 创建日期: 2022/04/24 11:34
 * @author HanWeiqi
 */

@Service
@Slf4j
public class ICartServiceImpl implements ICartService {

    @Autowired
    private CartCacheManager cartCacheManager;

    @Autowired
    private ShoppingCacheManager shoppingCacheManager;

    @Autowired
    private IProductService iProductService;

    @Override
    public CartListByIdResponse getCartListById(CartListByIdRequest request) {
        CartListByIdResponse cartListByIdResponse = new CartListByIdResponse();
        try {
            request.requestCheck();
            List<CartProductDto> cartCache = cartCacheManager.getCartsCache(request.getUserId());
            System.out.println(cartCache);
            cartListByIdResponse.setCartProductDtos(cartCache);
            cartListByIdResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            cartListByIdResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e){
            ExceptionProcessorUtils.wrapperHandlerException(cartListByIdResponse,e);
        }

        return cartListByIdResponse;
    }

    @Override
    public AddCartResponse addToCart(AddCartRequest request) {
        AddCartResponse addCartResponse = new AddCartResponse();
        CartProductDto cartProductDto = new CartProductDto();
        Long num = Long.valueOf(0);

        try {
            request.requestCheck();
            CartProductDto singleCartProduct = cartCacheManager.getSingleCartProduct(request.getUserId(), request.getItemId());
            if(singleCartProduct!=null){
                //singleCartProduct.setProductNum(singleCartProduct.getProductNum()+request.getNum());
                num = singleCartProduct.getProductNum();
            }

            ProductDetailDto checkCacheProductDetailDto = (ProductDetailDto) shoppingCacheManager.checkCache(GlobalConstants.PRODUCT_ITEM_CACHE_KEY, request.getItemId());
            if(checkCacheProductDetailDto!=null){
                BeanUtils.copyProperties(checkCacheProductDetailDto,cartProductDto);
                cartProductDto.setChecked("true");
                cartProductDto.setProductImg(checkCacheProductDetailDto.getProductImageBig());
                cartProductDto.setProductNum(num+request.getNum().longValue());
            } else{
                // 如果缓存中不存在商品的信息，就去数据库中进行查找
                ProductDetailRequest productDetailRequest = new ProductDetailRequest();
                productDetailRequest.setId(request.getItemId());
                ProductDetailResponse productDetail = iProductService.getProductDetail(productDetailRequest);
                ProductDetailDto productDetailDto = productDetail.getProductDetailDto();

                BeanUtils.copyProperties(productDetailDto,cartProductDto);
                cartProductDto.setChecked("true");
                cartProductDto.setProductImg(productDetailDto.getProductImageBig());
                cartProductDto.setProductNum(num+request.getNum().longValue());
            }
            cartCacheManager.setCartCache(request.getUserId(),request.getItemId(),cartProductDto);
            log.info(cartProductDto.toString());
            addCartResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            addCartResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e){
            ExceptionProcessorUtils.wrapperHandlerException(addCartResponse,e);
        }
        return addCartResponse;
    }

    @Override
    public UpdateCartNumResponse updateCartNum(UpdateCartNumRequest request) {

        UpdateCartNumResponse updateCartNumResponse = new UpdateCartNumResponse();
        try {
            request.requestCheck();
            CartProductDto singleCartProduct = cartCacheManager.getSingleCartProduct(request.getUserId(), request.getItemId());
            if(singleCartProduct!=null){
                singleCartProduct.setProductNum(request.getNum().longValue());
                singleCartProduct.setChecked(request.getChecked());
                cartCacheManager.setCartCache(request.getUserId(),request.getItemId(),singleCartProduct);
                log.info(singleCartProduct.toString());
            }

            updateCartNumResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            updateCartNumResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e){
            ExceptionProcessorUtils.wrapperHandlerException(updateCartNumResponse,e);
        }
        return updateCartNumResponse;
    }

    @Override
    public CheckAllItemResponse checkAllCartItem(CheckAllItemRequest request) {
        CheckAllItemResponse checkAllItemResponse = new CheckAllItemResponse();
        try{
            request.requestCheck();
            List<CartProductDto> cartProductDtos = cartCacheManager.getCartsCache(request.getUserId());
            if (cartProductDtos!= null && cartProductDtos.size()!=0){
                for (CartProductDto cartProductDto : cartProductDtos) {
                    cartProductDto.setChecked(request.getChecked());
                    cartCacheManager.setCartCache(request.getUserId(),cartProductDto.getProductId(),cartProductDto);
                    log.info(cartProductDto.toString());
                }
            }


            checkAllItemResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            checkAllItemResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e){
            ExceptionProcessorUtils.wrapperHandlerException(checkAllItemResponse,e);
        }
        return checkAllItemResponse;
    }

    @Override
    public DeleteCartItemResponse deleteCartItem(DeleteCartItemRequest request) {
        DeleteCartItemResponse deleteCartItemResponse = new DeleteCartItemResponse();
        try{
            request.requestCheck();
            cartCacheManager.deleteCartProduct(request.getUserId(),request.getItemId());
            deleteCartItemResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            deleteCartItemResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e){
            ExceptionProcessorUtils.wrapperHandlerException(deleteCartItemResponse,e);
        }
        return deleteCartItemResponse;
    }

    @Override
    public DeleteCheckedItemResposne deleteCheckedItem(DeleteCheckedItemRequest request) {
        List<CartProductDto> cartsCache = cartCacheManager.getCartsCache(request.getUserId());
        DeleteCheckedItemResposne deleteCheckedItemResposne = new DeleteCheckedItemResposne();
        try{
            request.requestCheck();
            if(cartsCache!=null&&cartsCache.size()!=0){
                for (CartProductDto cartProductDto : cartsCache) {
                    System.out.println(cartProductDto);
                    if ("true".equals(cartProductDto.getChecked())){
                        cartCacheManager.deleteCartProduct(request.getUserId(),cartProductDto.getProductId());

                    }
                }
            }
            deleteCheckedItemResposne.setCode(ShoppingRetCode.SUCCESS.getCode());
            deleteCheckedItemResposne.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e){
            ExceptionProcessorUtils.wrapperHandlerException(deleteCheckedItemResposne,e);
        }
        return deleteCheckedItemResposne;
    }
}
